/**  
* @Title: WechatRechargeServiceImpl.java
* @Package com.hjb.service.impl.Wechat
* @author <a href="mailto:125671254@qq.com">Pan Xinwen</a>panxinwen
* @date 2018年1月17日 下午11:21:55
* @version V1.0  
*/ 
package com.hjb.service.impl.Wechat;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hjb.service.Wechat.WechatRechargeService;
import com.hjb.service.config.constant.enums.FQorderStatus;
import com.hjb.service.domain.InstalmentOrder.InstalmentOrder;
import com.hjb.service.domain.recharge.SiteUserRecharge;
import com.hjb.service.domain.siteuser.SiteUser;
import com.hjb.service.gateway.baofoo.BaoFooPayService;
import com.hjb.service.mapper.InstalmentOrderMapper;
import com.hjb.service.mapper.SiteUserMapper;
import com.hjb.service.mapper.SiteUserRechargeMapper;
import com.hjb.service.utils.MapUtil;

/**
* @ClassName: WechatRechargeServiceImpl
* @Description: TODO(用户充值业务)
* @author <a href="mailto:125671254@qq.com">Pan Xinwen</a>panxinwen
* @date 2018年1月17日 下午11:21:55
*
*/
@Service
public class WechatRechargeServiceImpl implements WechatRechargeService {

	@Autowired
	private BaoFooPayService baoFooPayService; 
	@Autowired
	private SiteUserMapper siteUserMapper;
	@Autowired
	private SiteUserRechargeMapper siteUserRechargeMapper;
	@Autowired
	private InstalmentOrderMapper instalmentOrderMapper;
	
	@Override
	public Map<String, Object> getAuthCode(String userId , String amount,String instalmentId) {
		
		Map<String,Object> result = new HashMap<String,Object>();
		
		//获取用户信息
		SiteUser siteUser = new SiteUser();
//		siteUser.setUserId(Integer.parseInt(userId));
		siteUser = siteUserMapper.selectByPrimaryKey(Integer.parseInt(userId));

		Map<String,String> request = new HashMap<String,String>();
		request.put("ip", siteUser.getIp());
		request.put("bind_id", siteUser.getBindId());
		//生成充值流水号
		long timeMillis = System.currentTimeMillis();
		String orderNo = String.valueOf(timeMillis)+userId;
		request.put("transId", orderNo);
		request.put("amount", amount);
		
		Map<String, Object> dataMap = baoFooPayService.authPay(request);
		
		//业务处理
		if (dataMap == null){
			result.put("result_code", MapUtil.INTF_ERR_CODE);
			result.put("result_info", "获取验证码失败,请重试");
			return result;
		}else{
			//生成充值记录
			SiteUserRecharge siteUserRecharge = new SiteUserRecharge();
			siteUserRecharge.setOrderId(orderNo);
			siteUserRecharge.setInstalmentOrder(instalmentId);
			siteUserRecharge.setUserId(Integer.parseInt(userId));
			siteUserRecharge.setAmount(Long.parseLong(amount)*100);
			siteUserRecharge.setStatus(1);
			siteUserRecharge.setCreateTime(new Date());
			siteUserRecharge.setType(1);
			siteUserRecharge.setBankCard(siteUser.getBankCardNo());
			siteUserRecharge.setRetCode(String.valueOf(dataMap.get("resp_code")));
			siteUserRecharge.setRetMsg(String.valueOf(dataMap.get("resp_msg")));
			siteUserRecharge.setTradeNo(String.valueOf(dataMap.get("business_no")));
			siteUserRecharge.setCallbackMsg(String.valueOf(dataMap.get("dataJson")));
			siteUserRecharge.setOrderIdConf(String.valueOf(dataMap.get("trans_serial_no")));
			siteUserRecharge.setTradeNoConf(String.valueOf(dataMap.get("trans_id")));
			siteUserRechargeMapper.insert(siteUserRecharge);
			result.put("result_code", MapUtil.INTF_ERR_CODE);
//			if ("0000".equals(siteUserRecharge.getRetCode())){
//				resMap.put("result_code", MapUtil.INTF_SUC_CODE);
//			}
//			resMap.put("result_info", siteUserRecharge.getRetMsg());
			
			result.put("transId", orderNo);
		}
		
		return result;
	}

	/* (non-Javadoc)
	 * @see com.hjb.service.Wechat.WechatRechargeService#rechargeConfirm(java.lang.Long, java.lang.String)
	 */
	@Transactional
	@Override
	public Map<String, Object> rechargeConfirm(String orderId, String smsCode) {
		
		//获取支付交易流水号
		SiteUserRecharge record = new SiteUserRecharge();
		record.setOrderId(orderId);
		record = siteUserRechargeMapper.selectOne(record);
		
		Map<String, String> request = new HashMap<String,String>();
		request.put("smsCode", smsCode);
		request.put("businessNo", record.getTradeNo());
		
		//修改充值状态为充值成功
		record.setStatus(2);
		siteUserRechargeMapper.updateByPrimaryKeySelective(record);
		
		//更新当期还款状态为已还款
		InstalmentOrder instalmentOrder = new InstalmentOrder();
		instalmentOrder.setId(record.getInstalmentOrder());
		instalmentOrder.setStatus(FQorderStatus.FQ_STATUS_2.getValue());
		instalmentOrderMapper.updateByPrimaryKeySelective(instalmentOrder);
		
		return baoFooPayService.authPayConfirm(request);
	}

}
